package com.wxl52d41.Service.Impl;

import com.wxl52d41.Service.CacheTestService;
import com.wxl52d41.vo.TestUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * @ClassName: CacheTestServiceImpl
 * @Description: CacheTestServiceImpl
 * @Author: wang xiao le
 * @Date: 2022/12/05 19:51
 **/
@Service
public class CacheTestServiceImpl  implements CacheTestService {

    /*1. @Cacheable的几个属性详解：
     *       cacheNames/value：指定缓存组件的名字
     *       key：缓存数据使用的key,可以用它来指定。默认使用方法参数的值，一般不需要指定
     *       keyGenerator：作用和key一样，二选一
     *       cacheManager和cacheResolver作用相同：指定缓存管理器，二选一
     *       condition：指定符合条件才缓存，比如：condition="#id>3"
     *                   也就是说传入的参数id>3才缓存数据
     *      unless：否定缓存，当unless为true时不缓存，可以获取方法结果进行判断
     *      sync：是否使用异步模式*/
    //@Cacheable(cacheNames= "person")
    //@Cacheable(cacheNames= "person",key="#id",condition="#id>3")
    @Override
    @Cacheable(cacheNames = "user", key = "#userName")
    public TestUserVo queryUserByName(String userName) {
        System.out.println("查询" + userName + "号员工信息");
        TestUserVo userVo = new TestUserVo();
        userVo.setUserName(userName);
        userVo.setEmail("124@qq.com");
        return userVo;
    }

    @Override
    @CachePut(cacheNames = "user", key = "#userVo.userName")
    public TestUserVo updateUser(TestUserVo userVo) {

        System.out.println("userVo = " + userVo);
        return userVo;
    }
}
